<?php

namespace App\Models;

// use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;

// class STeacherInfo extends Model
class STeacherInfo extends Authenticatable
{
 
    use SoftDeletes;
    public $table = "s_teacher_info";

    protected $fillable = [
        'name','email', 'phone', 'password', 'headimg','type' ,'tags_zh_cn' ,'tags_en' , 'certification_zh_cn', 'certification_en', 'desc_en', 'desc_zh_cn', 'desc_voice' , 'created_at', 'updated_at'
    ];


    public function getTagsEnAttribute($value)
    {
        return explode(',', $value);
    }

    public function setTagsEnAttribute($value)
    {
        $this->attributes['tags_en'] = implode(',', $value);
    }



    public function getTagsZhCnAttribute($value)
    {
        return explode(',', $value);
    }

    public function setTagsZhCnAttribute($value)
    {
        $this->attributes['tags_zh_cn'] = implode(',', $value);
    }

    public function getCertificationZhCnAttribute($value)
    {
        return explode(',', $value);
    }

    public function setCertificationZhCnCnAttribute($value)
    {
        $this->attributes['certification_zh_cn'] = implode(',', $value);
    }


    static public function getAllTeacher()
    {
        return self::get();
    }
    
    static public function getList($teacher_type, $offset=0, $limit=4)
    {
    	return self::select('id', 'name', 'tags_zh_cn', 'type', 'headimg', 'certification_zh_cn' )
    			->where('type', $teacher_type)
    			->orderBy("id", "desc")
    			->offset($offset)
    			->limit($limit)
    			->get();
    }

    static public function getTeacherInfoForUser($teacher_id, $user_id)
    {
    	return self::leftJoin('s_stu_collect_info as sc', function($join) use($user_id) {
            $join->on('s_teacher_info.id', 'sc.teacher_id')
                    ->where('sc.user_id', $user_id);
                })
                ->where('s_teacher_info.id', $teacher_id)
    			->select('s_teacher_info.id', 's_teacher_info.name', 's_teacher_info.headimg','s_teacher_info.type' ,'s_teacher_info.tags_zh_cn' , 's_teacher_info.certification_zh_cn', 's_teacher_info.desc_zh_cn', 's_teacher_info.desc_voice' , 'sc.id as sid')
    			->first();
    }

    static public function getTeacherInfoForAppoint($teacher_id)
    {
        return self::where('id', $teacher_id)->select('headimg', 'name', 'tags_zh_cn', 'certification_zh_cn')
                ->first();
    }


}
